//
//  String+Regex.swift
//  正则表达式练习
//
//  Created by 龚欣 on 15/7/9.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit

extension String {

    func hreflink() -> (link : String?,text : String?){
        let pattern = ".*?\"(.*?)\".*?>(.*?)<.*?"
        
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
            let result = regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: characters.count))
            if result != nil {
                
                let r1 = result!.rangeAtIndex(1)
                let str1 = (self as NSString).substringWithRange(r1)
                
                let r2 = result!.rangeAtIndex(2)
                let str2 = (self as NSString).substringWithRange(r2)
                return (str1,str2)
            }
            
            return (nil,nil)
        }catch {
            print(error)
            return (nil,nil)
        }

    
    }
    
}